<template>
  <div>
    <el-button size="mini"
               type="success"
               style="margin-bottom: 10px"
               icon="el-icon-plus"
               @click="openTraceCententDailog">新增记录</el-button>
    <!-- 表格  -->
    <el-table :data="tableData"
              border
              stripe>
      <el-table-column type="expand"
                       size="small"
                       width="40">
        <template slot-scope="props"
                  size="small">
          <el-form label-position="left"
                   class="demo-table-expand">
            <el-form-item label="记录编号:">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="跟进内容:">
              <span>{{ props.row.content }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="content"
                       label="跟进内容"
                       header-align="center">
        <template slot-scope="props">
          <el-popover placement="bottom"
                      trigger="hover"
                      :content="props.row.content">
            <span slot="reference"
                  class="table-overflow-hidden">{{props.row.content}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="advancingState"
                       label="推进"
                       align="center"
                       width="90"></el-table-column>
      <el-table-column prop="projectMaturity"
                       label="成熟度"
                       align="center"
                       width="90"></el-table-column>
      <el-table-column prop="trackPepole"
                       label="跟进人员"
                       align="center"
                       width="100"></el-table-column>
      <el-table-column prop="tm"
                       label="跟进日期"
                       align="center"
                       width="160"></el-table-column>
      <el-table-column label="操作"
                       width="50"
                       align="center"
                       v-if="userInfo.isEdit">
        <template slot-scope="scope">
          <el-button type="text"
                     size="mini"
                     @click="delTraceInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :total="pagging.total"
                   :current-page="pagging.currentPage"
                   :page-sizes="pagging.sizes"
                   @size-change="sizeChange"
                   @current-change="pageChange">
    </el-pagination>
    <trace-centent-dailog :dialog-visible="dialogVisible"
                          :contract-id="contractId"
                          :advancing-state="advancingState"
                          :maturity="maturity"
                          @close="onClose"></trace-centent-dailog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { getMRecordsById, delMRecordById } from '@/api/maintenance'
import TraceCententDailog from './trace-centent-dailog'
export default {
  props: {
    contractId: { // 合同号
      type: String
    },
    maturity: {
      type: String // 项目成熟度
    },
    advancingState: {
      type: String // 项目推进
    }
  },
  data() {
    return {
      // 新增跟进记录dailog状态
      dialogVisible: false,
      // 表格数据
      tableData: [],
      // 分页属性
      pagging: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        sizes: [10, 30, 50, 100]
      }
    }
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  mounted() {
    this.search()
  },
  methods: {
    search() {
      this.pagging.currentPage = 1
      this.getTableData()
    },
    // 获取服务合同跟踪记录
    getTableData() {
      const params = {
        contractId: this.contractId,
        type: 1,
        pageSize: this.pagging.pageSize,
        pageIndex: this.pagging.currentPage
      }
      getMRecordsById(params).then(res => {
        if (res.data.success) {
          const result = res.data.data
          this.tableData = result.data
          this.pagging.total = result.totalCount
          this.pagging.currentPage = result.pageIndex
          this.pagging.pageSize = result.pageSize
        }
      })
    },
    // 删除跟进记录
    delTraceInfo(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      }).then(() => {
        delMRecordById({
          id: id
        }).then(res => {
          if (res.data.success) {
            const result = res.data.data
            if (result) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getTableData()
            }
          }
        })
      })
    },
    // 每页显示条数变更新
    sizeChange(size) {
      this.pagging.pageSize = size
      this.getTableData()
    },
    // 跳转到页面
    pageChange(curPage) {
      this.pagging.currentPage = curPage
      this.getTableData()
    },
    // 打开新增跟进记录界面
    openTraceCententDailog() {
      this.dialogVisible = true
    },
    // 关闭
    onClose() {
      this.dialogVisible = false
      this.getTableData()
    }
  },
  components: {
    TraceCententDailog
  },
  watch: {
    contractId() {
      this.search()
    }
  }
}
</script>
